package com.lsk.alipay.alipaylife;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.jfinal.plugin.activerecord.Db;
import com.lsk.alipay.constants.AlipayServiceEnvConstants;

import java.util.Map;

public class LimoAlipaySubmit {

    /**
     * 获取AccessToken
     * @param sParaTemp
     * @return
     */

    static String  ALIPAY_PUBLIC_KEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuuP6hHLRH4JFO+XKNXKBDiY7/pl9LfXEiHlBs108vV1jyzHvqt5i1I6I6xcH5W3TnQKyZGJmthbs9EnLf1R8rZhD44jQCgm1W/f8GMaDLuEdyr8/5nfQov3LDrY1BZbrMF7DMqbs8S7k6zml/RD30JtwAriBXX/vPa07LLQGKxRgEjmQpPIxHkZy8tEgtaP5MmCJ4wWVLSx2T1dQlRy11v6X9ohRcRL5OTq9gGE7YQK2BeIuy/vhlF51x+OTOLC70ZgaAte6ryVAe8amQN5Mco0aME59oA9rE4lE6X3Aim5y3s/SGbu5+GJXOv62S0oCbsQ/FodxSACgevIcVCOeXQIDAQAB";
    public static  String buildRequest(Map sParaTemp) throws AlipayApiException{
//        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, "RSA2");
        String sql="SELECT `value` from sys_config where element=";

        String applyAppId= Db.queryStr(sql+"'limoAppId'");

        String applyPublicKey=ALIPAY_PUBLIC_KEY;


        String applyPrivateKey= Db.queryStr(sql+"'limoAppPrivateKey'");

        AlipayClient alipayClient =
                new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                        applyAppId, applyPrivateKey, "json", "GBK",
                        applyPublicKey, "RSA2");
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setCode((String)sParaTemp.get("auth_code"));
        request.setGrantType("authorization_code");
        AlipaySystemOauthTokenResponse oauthTokenResponse=new AlipaySystemOauthTokenResponse();
        try {
             oauthTokenResponse = alipayClient.execute(request);
            return oauthTokenResponse.getAccessToken();
        } catch (AlipayApiException e) {
            //处理异常
            e.printStackTrace();
        }finally {
        }
        return null;
    }

    /**
     * 获取用户信息
     * @param accessToken
     * @return
     */
    public static String get(String accessToken){
//        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, "RSA2");
        String sql="SELECT `value` from sys_config where element=";

        String applyAppId= Db.queryStr(sql+"'limoAppId'");

        String applyPublicKey= ALIPAY_PUBLIC_KEY;


        String applyPrivateKey= Db.queryStr(sql+"'limoAppPrivateKey'");

        AlipayClient alipayClient =
                new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                        applyAppId, applyPrivateKey, "json", "GBK",
                        applyPublicKey, "RSA2");
        AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();

        try {
            AlipayUserInfoShareResponse userinfoShareResponse = alipayClient.execute(request, accessToken);
            return  userinfoShareResponse.getBody();
        } catch (AlipayApiException e) {
            //处理异常
            e.printStackTrace();
        }
        return null;
    }

}
